package com.shimao.celnet.payment.controller.api;

import com.baiwang.bop.client.IBopClient;
import com.baiwang.bop.client.impl.BopRestClient;
import com.baiwang.bop.request.impl.bizinfo.CompanySearchRequest;
import com.baiwang.bop.respose.entity.bizinfo.CompanySearchResponse;
import com.shimao.celnet.payment.model.request.SCodeNameConstant;
import com.shimao.celnet.payment.model.response.ErrorInfoEnum;
import com.shimao.celnet.payment.model.response.ResponseJson;
import com.shimao.celnet.payment.service.apiservice.CallOut_invOpenOutService;
import io.swagger.annotations.Api;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 *外部获取发票抬头信息
 * 通过公司名称
 * Created by Galen on 2018/11/29.
 */
@RestController
@RequestMapping(value = "/invoice")
@Api("接口：pay-162-外部获取发票抬头信息<OutInvCompSearchReqIn> 外部系统 → 支付系统，数据：外部获取发票抬头信息数据")
public class CallIn_OutInvCompSearchReqInController {

    protected Logger logger = Logger.getLogger(this.getClass());

    @Autowired
    private CallOut_invOpenOutService invOpenOutService;


    @RequestMapping(value = "/companySearchReq",method = RequestMethod.POST)
    public String invCompSearchReq(@RequestBody String companyName){
        ResponseJson retJson = new ResponseJson(ErrorInfoEnum.SUCCESS, "");
        logger.info("pay-162-外部获取发票抬头信息<OutInvCompSearchReqIn>请求JSON:" + companyName);
        if (StringUtils.isEmpty(companyName)) {
            logger.debug("缺少必要参数：" + companyName);
            retJson = new ResponseJson(ErrorInfoEnum.NOT_PARAMS, "companyName");
            return retJson.toString();
        }
        CompanySearchRequest searchRequest = new CompanySearchRequest();
        CompanySearchRequest request = new CompanySearchRequest();
        request.setCompanyName(companyName);//沙箱环境只能获取“百望股份”相关信息 --企业名称
        request.setSort("{\"frequency\":0}");//排序方式；0表示降序，1表示升序；
        //请求百望
        try {
            String token = invOpenOutService.getToken();
            logger.info("抬头请求参数：" + " token" + token + " companyName" + companyName);
            CompanySearchResponse response = this.getClient().execute(request,token,CompanySearchResponse.class);
            logger.info("百望云抬头响应信息：" + JSONObject.fromObject(response).toString());
            //如果查询结果为空，则提示抬头不存在
            if(!("[]".equals(JSONArray.fromObject(JSONObject.fromObject(response).get("result")).toString()))) {
                retJson.addBody("responseResult",JSONArray.fromObject(JSONObject.fromObject(response).get("result")));
            }else {
                retJson.setErrorMessage("该公司不存在");
            }
        }catch (Exception e){
            e.printStackTrace();
            logger.debug("百望异常参数：" + e.getMessage());
            retJson = new ResponseJson(ErrorInfoEnum.FAIL, e.getMessage());
        }
        return retJson.toString();
    }

    public IBopClient getClient(){
        SCodeNameConstant sCodeNameConstant =invOpenOutService.getBwSettingInfo();
        IBopClient client = new BopRestClient(sCodeNameConstant.getRateUrl(), sCodeNameConstant.getKey(), sCodeNameConstant.getSecret());
        return client;
    }
}
